package top.tagao.features_03_SteamApi;

import org.junit.jupiter.api.Test;
import top.tagao.features_02_References.Employee;
import top.tagao.features_02_References.EmployeeData;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/20 14:44
 */
public class SteamApi_03_end {

    //1-匹配与查找
    @Test
    public void test1() {
        List<Employee> employees = EmployeeData.getEmployees();

        //allMatch(Predicate p)——检查是否匹配所有元素。
        //练习：是否所有的员工的年龄都大于18
        boolean flag = employees.stream().allMatch(employee -> employee.getAge() > 18);
        System.out.println(flag);

        //anyMatch(Predicate p)——检查是否至少匹配一个元素。
        //练习：是否存在员工的工资大于 10000
        flag = employees.stream().anyMatch(employee -> employee.getSalary() > 10000);
        System.out.println(flag);

        //noneMatch(Predicate p)——检查是否没有匹配的元素。
        //练习：是否存在员工姓“雷”
        flag = employees.stream().noneMatch(employee -> employee.getName().startsWith("雷"));
        System.out.println(flag);

        //findFirst——返回第一个元素
        Optional<Employee> employee = employees.stream().findFirst();
        System.out.println(employee);

        //findAny——返回当前流中的任意元素
        Optional<Employee> any = employees.parallelStream().findAny();
        System.out.println(any);
    }

    @Test
    public void test2() {
        List<Employee> employees = EmployeeData.getEmployees();

        // count——返回流中元素的总个数
        long count = employees.stream().filter(e -> e.getSalary() > 5000).count();
        System.out.println(count);
        System.out.println();

        //max(Comparator c)——返回流中最大值
        //练习：返回最高的工资：
        Optional<Double> max = employees.stream().map(e -> e.getSalary()).max((m1, m2) -> (int) (m1 - m2));
        System.out.println(max);
        System.out.println();

        //min(Comparator c)——返回流中最小值
        //练习：返回最低工资的员工
        Optional<Employee> min = employees.stream().min((e1, e2) -> (int) (e1.getSalary() -  e2.getSalary()));
        System.out.println(min);
        System.out.println();

        //forEach(Consumer c)——内部迭代
        employees.stream().forEach(System.out::println);
        System.out.println();


        //使用集合的遍历操作 --外部迭代
        employees.forEach(System.out::println);
    }

    //2-归约
    @Test
    public void test3() {
        //reduce(T identity, BinaryOperator)——可以将流中元素反复结合起来，得到一个值。返回 T
        //练习1：计算1-10的自然数的和
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

        Integer sum1 = list.stream().reduce(0, Integer::sum);
        Integer sum2 = list.stream().reduce(0, (s1,s2)-> s1 + s2);
        System.out.println(sum1);
        System.out.println(sum2);
        System.out.println();

        //reduce(BinaryOperator) ——可以将流中元素反复结合起来，得到一个值。返回 Optional<T>
        //练习2：计算公司所有员工工资的总和
        List<Employee> employees = EmployeeData.getEmployees();
        Optional<Double> sumMoney1 = employees.stream().map(Employee::getSalary).reduce(Double::sum);
        Optional<Double> sumMoney2 = employees.stream().map(Employee::getSalary).reduce((s1,s2)->s1 + s2);
        System.out.println(sumMoney1);
        System.out.println(sumMoney2);
        //Optional<Double> sumMoney = salaryStream.reduce(Double::sum);


    }

    //3-收集
    @Test
    public void test4() {
        //collect(Collector c)——将流转换为其他形式。接收一个 Collector接口的实现，用于给Stream中元素做汇总的方法
        //练习1：查找工资大于6000的员工，结果返回为一个List或Set
        List<Employee> employees = EmployeeData.getEmployees();
        List<Employee> collect = employees.stream().filter(employee -> employee.getSalary() > 6000).collect(Collectors.toList());
        collect.forEach(System.out::println);

    }
}
